import { _decorator, Component, Node, RigidBodyComponent, Prefab, instantiate, CameraComponent, v3, Vec3, systemEvent, SystemEventType, EventKeyboard, macro, director } from "cc";
const { ccclass, property } = _decorator;

@ccclass("testScript")
export class testScript extends Component {
    /* class member could be defined like this */
    // dummy = '';

    @property(Node)
    ball01: Node = null

    @property(Prefab)
    ballPrefab: Prefab | null = null

    @property(Node)
    ballLayout: Node | null = null

    @property(Node)
    numLayout: Node | null = null

    @property(CameraComponent)
    camera: CameraComponent | null = null

    @property(Node)
    gun: Node = null

    onLoad() {

    }

    start() {
        systemEvent.on(SystemEventType.KEY_DOWN, this.keyDown, this)

    }


    addForce() {
        this.ball01.getComponent(RigidBodyComponent).applyTorque(cc.v3(0, 0, -100))
    }

    public addBall(ballNumber: number = 0) {
        let obj = instantiate(this.ballPrefab)
        obj.setPosition(cc.v3(0, 3, 0))

    }

    enableNumber() {
        console.log(this.ball01.children[0]);
        this.ball01.children[0].parent = this.numLayout
        console.log(this.numLayout.children);
        this.numLayout.children[0].active = true
    }

    addImpulse() {
        this.ball01.getComponent(RigidBodyComponent).applyImpulse(new Vec3(15, 0, 0))
    }

    keyDown(e: EventKeyboard) {
        switch (e.keyCode) {
            case macro.KEY.w: this.gun.translate(new Vec3(0, 0.1, 0)); break
            case macro.KEY.a: this.gun.translate(new Vec3(-0.1, 0, 0)); break
            case macro.KEY.d: this.gun.translate(new Vec3(0.1, 0, 0)); break
            case macro.KEY.s: this.gun.translate(new Vec3(0, -0.1, 0)); break
        }
    }


    restart() {

    }

}

